package com.habdqn.jspdemo.im.servlet.company;

import com.habdqn.jspdemo.im.enetity.Company;
import com.habdqn.jspdemo.im.service.CompanyService;
import com.habdqn.jspdemo.im.service.impl.CompanyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Administrator
 */
@WebServlet(name = "ListCompanyServlet",value = "/company/list")
public class ListCompanyServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //0.设置默认页码，默认每页显示数量
        int pageSize = 10;
        int pageNo = 1;
        //1.1获取请求中的pageNo
        String strPageNo = request.getParameter("pageNo");
        if(strPageNo!=null&&!"".equals(strPageNo)){
            pageNo = Integer.parseInt(strPageNo);
        }
        //1.2获取请求的公司模糊名称
        String likeName = request.getParameter("likeName");

        //2.调用业务方法获
        CompanyService service = new CompanyServiceImpl();
        //2.1默认显示首页
        List<Company> companies = service.findPage(pageNo,pageSize,likeName);
        //2.2获取总数据量
        int totalCount = service.getCount(likeName);
        //2.3计算总页数
        int pageCount = (int)Math.ceil((double)totalCount/pageSize);

        //3.将数据封装到request，转发到页面
        request.setAttribute("companies",companies);
        request.setAttribute("totalCount",totalCount);
        request.setAttribute("pageCount",pageCount);
        request.setAttribute("pageNo",pageNo);
        request.setAttribute("likeName",likeName);
        request.getRequestDispatcher("/company/list.jsp").forward(request,response);
    }
}
